<?php
/**
 * 队列消费端
*/

namespace houze;

class Queue 
{
    /**
     * 驱动实例
    */
    private $QueueDriver;
    /**
     * 驱动
    */
    private $DriverName = 'default';

    /**
     * 初始化配置
    */
    public function __construct($DriverName = null) {
        if ($DriverName !== null) $this->DriverName = $DriverName;
    }

    /**
     * 指定队列
    */
    public function store($type) {
        $this->DriverName = $type;
        return $this;
    }

    /**
     * 驱动方法注入
    */
    public function __call($name, $arguments=[]) {
        $this->saveConfig();
        return call_user_func_array([$this->QueueDriver, $name], $arguments);
    }

    /**
     * 更新驱动实例
    */
    private function saveConfig() {
        $config = \houze\Config::get('queue.'.$this->DriverName);
        $class = '\houze\queue\driver\\'. ucfirst(strtolower($config['type']));
        $this->QueueDriver = new $class($config);
    }
}